#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "rngs.h"
#include "testhelpers.h"

// this program tests the functionality of the adventurer card
void setHand(struct gameState *state){
    int i;
    state->handCount[0] = 0;
    for (i = 0; i < 10; i++){
        state->hand[0][i] = randomCard;
        state->handCount[0]++;
    }
    state->hand[0][0] = adventurer;
}

void setDeck(struct gameState *state){
    int i;
    state->deckCount[0] = 0;
    for (i = 0; i < 50; i++){
        state->deck[0][i] = randomCard;
        state->deckCount[0]++;
    }
    state->deck[0][25] = copper;
    state->deck[0][30] = silver;
    state->deck[0][35] = gold;
}

// returns true if there are more than 2 treasure cards in the deck
int countTreasure(struct gameState *state){
    int i;
    int cnt = 0;
    for(i = 0; i < state->deckCount[0]; i++){
        if (state->deck[0][i]== copper || state->deck[0][i] == silver || state->deck[0][i] == gold){
            cnt++;
        }
        if (cnt > 2){
            return 0;
        }
    }
    return 1;
}

int main(){
    struct gameState *state = malloc(sizeof(struct gameState));
    int i;
    int j;
    int numTests = 20;
    int testhandcount;
    setState(state);
    setDeck(state);
    setHand(state);
    resetFlag();

    printf("Starting Adventuerer Test\n");

    for (i = 0; i < numTests; i++){
        if (countTreasure(state) == 1){
            printf("Test iteration skipped.  There are fewer than 2 treasure cards, this results in an infinite loop\n");
        fflush(stdout);
        }
        else {
            testhandcount = state->handCount[0];
            cardEffect(adventurer, 0, 0, 0, state, 0, 0);
            if (state->handCount[0] != testhandcount + 1) //+1 because we discard a card and gain 2
                assertMessage("Hand Count is %d when it should be %d\n", state->handCount[0], testhandcount+1);
            if (state->hand[0][state->handCount[0]-2] != copper && state->hand[0][state->handCount[0]-2] != silver && state->hand[0][state->handCount[0]-2] != gold){
                printf("Added cards are not treasures\n");
                incState();
            }
            if (state->hand[0][state->handCount[0]-1] != copper && state->hand[0][state->handCount[0]-1] != silver && state->hand[0][state->handCount[0]-1] != gold){
                printf("Added cards are not treasures\n");
                incState();
            }
            if (state->playedCards[0] != adventurer){
                assertMessage("Played card is %d, but should be %d\n", state->playedCards[0], adventurer);
            }
        }
        setDeck(state);
        setHand(state);
        state->hand[0][state->handCount[0]] = -1;
        state->hand[0][state->handCount[0] + 1] = -1;
    }

    if(testState() == 0)
        printf("Adventurer Card Effect Test Passed\n");
    else
        printf("Adventurer Card Effect Test Failed %d times\n", testState());

    free(state);
    return 0;
}
